/*
  MinCC - Minimalist Compiler Collection
  Copyright (C) 2009 Scott Lawrence
  
  MinCC is free software: you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  MinCC is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU General Public License
  along with mincc.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <string.h>

#include "cmdline.hpp"
#include "state.hpp"
#include "compile.hpp"
#include "interpret.hpp"
#include "error.hpp"

void init();

int main(int argc,char *argv[]) {
    if(argc==1) {
        noargs();
        return 0;
    }
    //create expanded arguments array
    char *args[MAX_ARGS];
    int count=0;
    for(int i=1;i<argc;i++) {
        if(argv[i][0]=='-' && argv[i][1]=='-') {
            //long argument, just add
            args[count++]=argv[i];
        } else if(argv[i][0]=='-') {
            //short argument
            //go through each character and add
            for(int j=1;j<strlen(argv[i]);j++) {
                char *t=new char[2];
                t[0]='-';
                t[1]=argv[i][j];
                args[count++]=t;
            }
        } else {
            //raw argument, just add
            args[count++]=argv[i];
        }
    }

    int filecount=0;
    char *files[MAX_FILES];
    //parse the arguments
    for(int i=0;i<count;i++) {
        if(args[i][0]=='-')
            execArg(count,args,i);
        else {
            //its a raw argument, like a file to compile
            files[filecount++]=args[i];
        }
    }

    //and handle files here
    if(global_state.compile)
        doCompile();
    else
        doInterpret();

    return 0;
}

void init() {
    initState();
}
